package com.guohe3.just.service.impl;

import com.guohe3.just.DO.Book;
import com.guohe3.just.service.LibrarayService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LibrarayServiceImpl implements LibrarayService {

    public List<Book> GetLibToplist(){
        ArrayList<Book> books = new ArrayList<>();
        Connection con = Jsoup.connect("http://lib.just.edu.cn:8080/top/top_lend.php").method(Connection.Method.GET);
        try{
            Connection.Response s = con.execute();
            Document document = Jsoup.parse(s.body());
            Elements links = document.select("td[class=whitetext]");
            //System.out.println(links);
            int number=0;
            if(links.isEmpty()) return null;
            for(int i=0;i<100;i++){
                Book book = new Book();
                book.setRank(links.get(number++).text());
                book.setName(links.get(number++).text());
                book.setAuthor(links.get(number++).text());
                book.setInfo(links.get(number++).text());
                book.setBooknumber(links.get(number++).text());
                book.setNumber(links.get(number++).text());
                book.setTimes(links.get(number++).text());
                book.setBili(links.get(number++).text());
                books.add(book);
            }
        }catch (IOException e){

        }
        return books;
    }
}
